﻿using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using ZilLion.Service.Core.DapperWrapper;
using ZL.CRM.CommonEntity.Peizh;
using ZL.Web.Utility.Cache;
using ZLWeb.WebApiEntityBase.Cache;

namespace ZLWeb.WebApiInfrastructure.Base
{
    public class GloabConfigCacheLoader
    {
        public IConfigCacheRepository ConfigCacheRepository { get; set; }
        public GloabConfigCacheLoader(IConfigCacheRepository ConfigCacheRepository)
        {
            this.ConfigCacheRepository = ConfigCacheRepository;
        }

        public static GloabConfigCacheLoader CreateGloabConfigCacheLoader(IDbContext DbContext)
        {
            return new GloabConfigCacheLoader(new ConfigCacheRepository(DbContext));
        }

        public void LoadCache()
        {
            SERedisHelper.KeyDelete(JltyKzCacheItem.CacheKey);
            SetJltyKzCache();
            SERedisHelper.KeyDelete(JltyZklbCacheItem.CacheKey);
            SetJltyZklbCache();
            SERedisHelper.KeyDelete(WebApiEntityBase.Cache.ZzinfocsCacheItem.CacheKey);
            SetChargeInfo();
            SERedisHelper.KeyDelete(GxcspzCacheItem.CacheKey);
            SetGxcsCache();
            SERedisHelper.KeyDelete(ZlfixedlistCacheItem.CacheKey);
            SetZlfixedlistCache();
            SERedisHelper.KeyDelete(ZlcompanylistCacheItem.CacheKey);
            SetZlcompanylistCache();
            LoadCheckConfigCache();
        }

        private ZlcompanylistCacheItem SetZlcompanylistCache()
        {
            ZlcompanylistCacheItem item = ConfigCacheRepository.GetZlcompanylistCacheItem();
            SERedisHelper.Set(ZlcompanylistCacheItem.CacheKey, item, null);
            return item;
        }

        public GxcspzCacheItem SetGxcsCache()
        {
            GxcspzCacheItem item = ConfigCacheRepository.GetGxcspzCacheItemConfigItems();
            SERedisHelper.Set(GxcspzCacheItem.CacheKey, item, null);
            return item;
        }

        public JltyKzCacheItem SetJltyKzCache()
        {
            JltyKzCacheItem item = ConfigCacheRepository.GetJltycrmkykztConfigItems();
            SERedisHelper.Set(JltyKzCacheItem.CacheKey, item,null);
            return item;
        }

        public JltyZklbCacheItem SetJltyZklbCache()
        {
            JltyZklbCacheItem item = ConfigCacheRepository.GetJltycrmkyzklbtConfigItems();
            SERedisHelper.Set(JltyZklbCacheItem.CacheKey, item, null);
            return item;
        }

        public ZL.CRM.CommonEntity.Peizh.ZzinfocsCacheItem SetChargeInfo()
        {
            ZL.CRM.CommonEntity.Peizh.ZzinfocsCacheItem Zzinfoc = ConfigCacheRepository.GetChargeInfo();
            SERedisHelper.Set(ZL.CRM.CommonEntity.Peizh.ZzinfocsCacheItem.CacheKey, Zzinfoc, null);
            return Zzinfoc;
        }

        public ZlfixedlistCacheItem SetZlfixedlistCache()
        {
            ZlfixedlistCacheItem zlfixedlistc = ConfigCacheRepository.GetZlfixedlistCacheItemConfigItems();
            SERedisHelper.Set(ZlfixedlistCacheItem.CacheKey, zlfixedlistc, null);
            return zlfixedlistc;
        }

        public ZL.CRM.CommonEntity.Peizh.ZzinfocsCacheItem GetZzinfocsCacheItem()
        {
            return SERedisHelper.Get<ZL.CRM.CommonEntity.Peizh.ZzinfocsCacheItem>(ZL.CRM.CommonEntity.Peizh.ZzinfocsCacheItem.CacheKey);
        }

        public ZlcompanylistCacheItem GetZlcompanylistCacheItem()
        {
            ZlcompanylistCacheItem item = SERedisHelper.Get<ZlcompanylistCacheItem>(ZlcompanylistCacheItem.CacheKey);
            if (item == null) return SetZlcompanylistCache();
            return item;
        }

        public GxcspzCacheItem GetGxcspzCacheItem()
        {
            GxcspzCacheItem item = SERedisHelper.Get<GxcspzCacheItem>(GxcspzCacheItem.CacheKey);
            if (item == null) return SetGxcsCache();
            return item;
        }

        public JltyZklbCacheItem GetJltyZklbCache()
        {
            JltyZklbCacheItem item= SERedisHelper.Get<JltyZklbCacheItem>(JltyZklbCacheItem.CacheKey);
            if (item == null) return SetJltyZklbCache();
            return item;
        }

        public JltyKzCacheItem GetJltyKzCache()
        {
            JltyKzCacheItem item=SERedisHelper.Get<JltyKzCacheItem>(JltyKzCacheItem.CacheKey);
            if (item == null) return SetJltyKzCache();
            return item;
        }

        public ZlfixedlistCacheItem GetZlfixedlistCacheItem()
        {
            ZlfixedlistCacheItem item = SERedisHelper.Get<ZlfixedlistCacheItem>(ZlfixedlistCacheItem.CacheKey);
            if (item == null) return SetZlfixedlistCache();
            return item;
        }


        public CrmYwDbItem GetCrmYwDbCacheItem()
        {
            CrmYwDbItem item = SERedisHelper.Get<CrmYwDbItem>(CrmYwDbItem.CacheKey);
            if (item == null)
            {
                throw new System.Exception("redis中未配置CRM配置库信息。");
            }
            return item;
        }

        public void LoadCheckConfigCache()
        {
            TaskFactory tf = new TaskFactory();
            Task task1 = tf.StartNew(CheckConfigCaches);
        }

        

        public void CheckConfigCaches()
        {
            try
            {
                while (true)
                {
                    Thread.Sleep(30000);
                    int GroupVersion1 = ConfigCacheRepository.CheckJltycrmkykztItems();
                    var kyzitem = GetJltyKzCache();
                    if (GroupVersion1 > kyzitem.kykztslogsn)
                    {
                        SERedisHelper.KeyDelete(JltyKzCacheItem.CacheKey);
                        SetJltyKzCache();
                    }
                    int btGroupVersion2 = ConfigCacheRepository.CheckJltycrmkyzklbtItems();
                    var btitem = GetJltyZklbCache();
                    if (btGroupVersion2 > btitem.kyzklbtslogsn)
                    {
                        SERedisHelper.KeyDelete(JltyZklbCacheItem.CacheKey);
                        SetJltyZklbCache();
                    }
                    int GroupVersion3 = ConfigCacheRepository.CheckChargeInfo();
                    var gxcsitem = GetGxcspzCacheItem();
                    if (GroupVersion3 > gxcsitem.gxcseslogsn)
                    {
                        SERedisHelper.KeyDelete(ZL.CRM.CommonEntity.Peizh.ZzinfocsCacheItem.CacheKey);
                        SetChargeInfo();
                    }
                    int GroupVersion4 = ConfigCacheRepository.CheckGxcspz();
                    var item = GetZzinfocsCacheItem();
                    if (GroupVersion4 > item.kinfocslogsn)
                    {
                        SERedisHelper.KeyDelete(GxcspzCacheItem.CacheKey);
                        SetGxcsCache();
                    }
                    int GroupVersion5 = ConfigCacheRepository.CheckZlcompanylistCacheItem();
                    var companyitem = GetZlcompanylistCacheItem();
                    if (GroupVersion5 > companyitem.Zlcompanylogsn)
                    {
                        SERedisHelper.KeyDelete(ZlcompanylistCacheItem.CacheKey);
                        SetZlcompanylistCache();
                    }
                }
            }
            catch (System.Exception ex)
            {

            }
        }
    }
}
